package com.gitee.hljdrl.dialog.visitor;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.gitee.hljdrl.dialog.R;
import com.gitee.hljdrl.dialog.UISheetDialog;
import com.gitee.hljdrl.dialog.basis.OnItemClickListener;
import com.gitee.hljdrl.dialog.basis.OnSheetListener;
import com.gitee.hljdrl.dialog.entity.SheetEntity;
import com.gitee.hljdrl.dialog.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

import com.gitee.hljdrl.dialog.adapter.SheetAdapter;

public class SheetVisitor extends BasisViewPartVisitor {

    private int mDefaultItemHeight = 50;
    protected List<SheetEntity> mList;
    protected RecyclerView.Adapter mAdapter;
    protected RecyclerView.LayoutManager mLayoutManager;
    protected OnSheetListener onSheetListener;
    protected CharSequence mTitle;
    protected boolean mShowTitle = true;

    public SheetVisitor(Context context) {
        super(context);
        setLayout(R.layout.dialog_sheet_part);
        setDialogTheme(R.style.UISheetDialog);
    }

    @Override
    protected boolean enablePromptVisit() {
        return false;
    }

    @Override
    public void onViewVisit(Dialog dialog, View view) {
        final Context context = dialog.getContext();
        RecyclerView rvView = view.findViewById(R.id.rv_list);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        if (mAdapter == null) {
            if (mList == null) {
                mList = new ArrayList<>();
            }
            SheetAdapter adapter = new SheetAdapter(context, mList);
            if (!TextUtils.isEmpty(mTitle)) {
                tvTitle.setText(mTitle);
                adapter.setFirstDrawable(R.drawable.dialog_sheet_item_selector);
            }
            adapter.setOnItemClickListener(new OnItemClickListener<SheetEntity>() {
                @Override
                public void onItemClick(int position, SheetEntity sheetBean) {
                    if (onSheetListener != null) {
                        onSheetListener.onSheetSelected(position);
                    }
                    UIUtil.dismissDialog(dialog);
                }
            });
            mAdapter = adapter;
        } else {
            if (!TextUtils.isEmpty(mTitle)) {
                tvTitle.setText(mTitle);
            }
        }
        calculationMaxHeight(rvView, mAdapter, mDefaultItemHeight);
        if (mLayoutManager == null) {
            mLayoutManager = new LinearLayoutManager(context);
        }
        rvView.setLayoutManager(mLayoutManager);
        rvView.setAdapter(mAdapter);
        View cancel = view.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSheetListener != null) {
                    onSheetListener.onSheetCancel();
                }
                UIUtil.dismissDialog(dialog);
            }
        });
        tvTitle.setVisibility(mShowTitle ? View.VISIBLE : View.GONE);
        super.onViewVisit(dialog, view);
    }

    public SheetVisitor setOnSheetListener(OnSheetListener onSheetListener) {
        this.onSheetListener = onSheetListener;
        return this;
    }

    public SheetVisitor setShowTitle(boolean showTitle) {
        this.mShowTitle = showTitle;
        return this;
    }

    public SheetVisitor setList(List<SheetEntity> list) {
        this.mList = list;
        return this;
    }

    public SheetVisitor setItemHeight(int itemHeight) {
        mDefaultItemHeight = itemHeight;
        return this;
    }

    public SheetVisitor setAdapter(RecyclerView.Adapter adapter) {
        this.mAdapter = adapter;
        return this;
    }

    public SheetVisitor setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        this.mLayoutManager = layoutManager;
        return this;
    }

    /**
     * 计算ListView的最大高度
     *
     * @param view       视图
     * @param adapter    适配器
     * @param itemHeight 行高
     */
    private void calculationMaxHeight(RecyclerView view, RecyclerView.Adapter adapter, int itemHeight) {
        if (0 <= itemHeight) {
            itemHeight = mDefaultItemHeight;
        }
        int contentHeight = adapter.getItemCount() * itemHeight;
        int maxHeight = UIUtil.screenHeight() / 2;
        if (contentHeight > maxHeight) {
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, maxHeight));
        }
    }

    @Override
    protected Dialog createDialog(Context context, int theme) {
        UISheetDialog dialog = new UISheetDialog(context, theme);
        dialog.setViewPartVisitor(this);
        return dialog;
    }
}
